package cn.itsource.org.controller;

import cn.itsource.org.domain.Shop;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.org.service.IShopService;
import cn.itsource.util.AjaxResult;
import cn.itsource.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class ShopController {

    @Autowired
    private IShopService shopService;

    @GetMapping //@RequestMapping( method = {RequestMethod.GET} )
    public List<Shop> getAll(){
        return  shopService.queryAll();
    }

    //RequestBody 接收请求的json对象
    @PutMapping //@RequestMapping( method = {RequestMethod.PUT} )
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult saveOrUpdate(@RequestBody Shop Shop){
        try {
            if (Shop.getId()==null){
                shopService.save(Shop);
            }else {
                shopService.update(Shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }
    //前端发送请求  loclhost:8080/Shop/2
    @DeleteMapping("/{id}")    //@RequestMapping( method = {RequestMethod.DELETE} )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }


    @GetMapping("/{id}")
    public Shop queryById(@PathVariable("id") Long id){
        return shopService.queryById(id);
    }

    @PostMapping
    public PageInfo<Shop> page(@RequestBody ShopQuery query){
        return  shopService.queryPage(query);
    }

    @PostMapping("/settlement")
    public AjaxResult settlement( @RequestBody Shop shop){
        System.out.println(shop);
        try {
            shopService.settlement(shop);
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }


}
